//
//  CLVCNovelCatalogue.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/5/15.
//

#import "CLVCNovelCatalogue.h"
#import <ChuckTableView/ChuckTableView.h>

@interface CLVCNovelCatalogue () <ChuckDelegate>
@property (nonatomic, strong) ChuckTableView *tableView;
@end

@implementation CLVCNovelCatalogue

- (void)viewDidLoad {
    [super viewDidLoad];
    JSLog(@"[native] 目录页<%p>%@",self.book,self.book.bookid);
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (self.book.catalogue.count) {
        [self.tableView addModels:self.book.catalogue];
        [self.tableView reloadData];
    }
    WEAK_SELF;
    [self.book addObserverWithUpdateBlock:^{
        [weakSelf.tableView addModels:weakSelf.book.catalogue];
        [weakSelf.tableView reloadData];
    }];
}

- (ChuckTableView *)tableView {
    if (!_tableView) {
        WEAK_SELF;
        _tableView = [[ChuckTableView alloc]initWithFrame:self.contentView.bounds style:UITableViewStyleGrouped heightForRow:^CGFloat(id cell, id model, NSIndexPath *indexPath) {
            return 44;
        } vcDelegate:self configureBlock:^(UITableViewCell *cell, CLReadChapterModel *model, NSIndexPath *indexPath) {
            cell.textLabel.text = model.name;
            cell.textLabel.textColor = NULLString(model.content) ? [UIColor lightGrayColor] : [UIColor blackColor];
        } cellDidselectConfig:^(UITableViewCell *cell, CLReadChapterModel *model, NSIndexPath *indexPath) {
            [weakSelf.tableView deselectRowAtIndexPath:indexPath animated:YES];
            [CLNavigationService dismissControllerAnimated:YES completion:^{
                if ([weakSelf.delegate respondsToSelector:@selector(cl_novelViewOpenChapter:indexPath:)]) {
                    [weakSelf.delegate cl_novelViewOpenChapter:model indexPath:indexPath];
                }
            }];
        }];
        CLAdjustsScrollViewInsets_Never(_tableView);
        _tableView.tableHeaderView = [[UIView alloc]init];
        [self.contentView addSubview:_tableView];
    }
    return _tableView;
}
@end
